# -*- coding: utf-8 -*-
"""
Created on Mon Nov 30 20:02:03 2015

@author: liangrongli
"""

import urllib
import urllib2
import re
#import thread
#import time

class QSBK:
    def __init__(self):
        self.pageindex = 1
        self.user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0'
        #初始化headers
        self.headers = {'User-Agent':self.user_agent}
        #存放段子的变量，每个元素是每一页的段子们
        self.stories = []
        #存放程序是否继续运行的变量
        self.enable = False
    #传入某一页的索引获得页面内容
    def getpage(self,pageindex):
        try:
            url = 'http://www.qiushibaike.com/hot/page/'+str(pageindex)
            request = urllib2.Request(url,headers=self.headers)
            reponse = urllib.urlopen(request)
            page_content = reponse.read().decode('utf-8')
            return page_content
        except urllib2.URLError,e:
            if hasattr(e,'reason'):
                print u"链接糗事百科失败，错误原因：",e.reason
                return None
                
    #传入某页代码，返回本页的段子列表
    def getPageItems(self,pageindex):
        page_content = self.getpage(pageindex)
        if not page_content:
            print "load error...."
            return None
        pattern = re.compile(r'<h2>(.*?)</h2>.*?<div.*?content">(.*?)<!--.*?</div>.*?<span.*?stats-vote">.*?<i.*?number">(.*?)</i>.*?</span>',re.S)
        items = re.findall(pattern,page_content)
        page_stories = []
        for item in items:
            #page_stories.append([item[0].strip(),item[1].strip(),item[2].strip()])
            page_stories.append([item[0],item[1],item[2]])
        return page_stories
        
    def loadPage(self):
        if self.enable == True:
            if len(self.stories) < 2:
                pages_stories = self.getPageItems(self.pageindex)
                if pages_stories:
                    self.stories.append(pages_stories)
                    self.pageindex += 1

    def getOneStory(self,pagestories,page):
        for story in pagestories:
            input = raw_input("> ")
            self.loadPage()
            if input == "Q":
                self.enable = False
                return
            print u"第%s页\t发布人%s\t%s\t评论数：%s" %(page,story[0],story[1],story[1])

    def start(self):
        print u"正在读取糗事百科，按回车查看新段子，按’Q‘退出"
        self.enable = True
        self.loadPage()
        now_page = 0
        while self.enable:
            if len(self.stories) > 0:
                #从全局list中获取一页的段子
                pageStories = self.stories[0]
                #当前读到的页数加一
                now_page += 1
                #将全局list中第一个元素删除，因为已经取出
                del self.stories[0]
                #输出该页的段子
                self.getOneStory(pageStories,now_page)


spider = QSBK()
spider.start()